package projbd;

import java.io.IOException;
import java.sql.Statement;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

public class XMLReconstructor {
    
    private Writer xmlWriter;
    private String orderStatement;
    private Statement statement;
    private Connection connection;
    private Stack<String> tagStack = new Stack<String>();
    
    public XMLReconstructor(String file) throws UnsupportedEncodingException, FileNotFoundException, SQLException, IOException {
                
        connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/projbd", "postgres", "srx3325");
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                               ResultSet.CONCUR_UPDATABLE);
        xmlWriter = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        orderStatement = "SELECT * FROM order_size_edges_2 ORDER BY id";
    }
    
    public void reconstruct() throws SQLException, IOException {
        ResultSet result = statement.executeQuery(orderStatement);
        
        result.first();
        while (!result.isAfterLast()) {
            String value = result.getString("value");
            String attributes = result.getString("attributes");
            if (attributes != null) attributes = attributes.trim();
            String datatype = result.getString("datatype");
            
            if (datatype.equals("tag")) {
               tagStack.push(value);
                if (attributes != null) {
                    xmlWriter.write("<" + value + " " + attributes + ">\n");
                } else {
                    xmlWriter.write("<" + value + ">\n");
                }
            } else if (datatype.equals("text")) {
                xmlWriter.write(value + "\n");
                String father_id;
                if (result.isLast()) {
                    father_id = result.getString("parent_id");
                } else {
                    result.next();
                    father_id = result.getString("parent_id");
                    result.previous();
                }
                
                String getFatherValue = "SELECT * FROM order_size_edges_2 WHERE id = " + "'" + father_id + "'";
                
                int lastRow = result.getRow();
                result = statement.executeQuery(getFatherValue);
                if (result != null) {
                    result.first();
                    String fValue = result.getString("value");

                    while (!(tagStack.peek()).equals(fValue)) {
                        String stackValue = tagStack.pop();
                        if (attributes != null) {
                            xmlWriter.write("</" + stackValue + " " + attributes + ">\n");
                        } else {
                            xmlWriter.write("</" + stackValue + ">\n");
                        }
                    }
                }
                result = statement.executeQuery(orderStatement);
                result.absolute(lastRow);
                if (result.isLast()) {
                    String sValue = tagStack.pop();
                    if (attributes != null) {
                        xmlWriter.write("</" + sValue + " " + attributes + ">\n");
                    } else {
                        xmlWriter.write("</" + sValue + ">\n");
                    }
                }
            }
            result.next();
        }
        xmlWriter.flush();
        xmlWriter.close();
    }
    
    public static void main(String[] args) {
        
        try {
            XMLReconstructor xmlReconstructor = new XMLReconstructor("C:\\bd\\hamlet-reconstructed.xml");
            xmlReconstructor.reconstruct();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(XMLReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(XMLReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLReconstructor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
